<?php
/**
 * @Author: king
 * @Last Modified by:   king
 * Date: 2018/6/19
 * Time: 22:40
 */
namespace App\Tools;


use Alipay\AopClient;
use App\Tools\Orders as OrdersLib;
use Jdpay\XMLUtil;

/**
 * @title 支付签名验证
 * Class PayCheckSign
 * @package App\Tools
 */
class PayCheckSign
{
    /**
     * 微信支付验签
     * @param $data
     * @param $app_key
     * @return bool
     */
    public function weixin($data,$app_key)
    {
        ksort($data);
        $buff = '';
        foreach ($data as $k => $v)
        {
            if($k != 'sign' && !empty($v))
            {
                $buff .= $k . '=' . $v . '&';
            }
        }

        $stringSignTemp = $buff . 'key='.$app_key;

        $sign = strtoupper(md5($stringSignTemp));

        if($sign == $data['sign']) return true;
       
        return false;
    }

    /**
     * 支付宝支付验签
     * @param $data array 支付宝返回的
     * @return bool
     */
    public function ali($data)
    {
        $aop = new AopClient();

        $aliConfig = OrdersLib::getPayConfig('ali');

        $aop->alipayrsaPublicKey = $aliConfig['alipay_public_key'];

        $flag = $aop->rsaCheckV1($data, NULL, "RSA2");

        if($flag) return true;

        return false;
    }

    /**
     * 京东支付验签
     * @param $data
     * @param $resData
     * @param $jdConfig array 京东支付相关配置
     * @return bool
     */
    public function jd($data, &$resData, $jdConfig)
    {
        $flag = XMLUtil::decryptResXml($data, $resData, $jdConfig);

        return $flag;
    }
}